//
//  HLYiYanContentViewController.m
//  Various
//
//  Created by HarwordLiu on 16/3/29.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "HLYiYanContentViewController.h"
#import "HLYiYanModel.h"
#import "HLYiTanContentCollectionViewCell.h"


@interface HLYiYanContentViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGRect frameCell;

@end

@implementation HLYiYanContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  防止自动调节系统自动collectionView的大小
     */
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatSubView];
    [self showAnimation];
    

    
}

- (void)showAnimation {
//    self.frameCell = self.cell.frame;
    self.snapShotView.frame = [self.cell convertRect:self.cell.bounds toView:nil];
    
    _snapShotView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    _snapShotView.layer.shadowOffset = CGSizeMake(1, 1);//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    _snapShotView.layer.shadowOpacity = 1;//阴影透明度，默认0
    _snapShotView.layer.shadowRadius = 5;//阴影半径，默认3
    
    self.frameCell = self.snapShotView.frame;
    [self.view addSubview:self.snapShotView];
    

    
    self.navigationItem.title = @"荐";
    [self.tabBarController hiddenTabBar:YES];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.navigationController.navigationBar.frame = CGRectMake(0, -64, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
        self.snapShotView.frame = CGRectMake(50 * sWEIGHT, 80 * sHEIGHT, self.frameCell.size.width + 20, self.frameCell.size.height + 20);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.snapShotView.alpha = 0;
            self.collectionView.alpha = 1;
        }];
    }];
}

- (void)creatSubView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(414 * sWEIGHT, 736 * sHEIGHT);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 414 * sWEIGHT, 736 * sHEIGHT) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.alpha = 0;
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];

    
    [self.collectionView registerClass:[HLYiTanContentCollectionViewCell class] forCellWithReuseIdentifier:YIYANCONTENTCELL];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 20, 50 * sWEIGHT, 50 * sHEIGHT);
    [backBtn setImage:[UIImage imageNamed:@"backBtnYiYan"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}

- (void)clickBackBtn:(UIButton *)sender {
    [self.delegate changeCellIndexPath:self.indexPath sentSnapShotView:self.snapShotView];
    [self dismissAnimation];
}
#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.indexPath = [NSIndexPath indexPathForRow:scrollView.contentOffset.x / (sWEIGHT * 414) inSection:0];
    
}

#pragma mark - CollectionView dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"+++++++++");
    HLYiTanContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YIYANCONTENTCELL forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.arrModel[indexPath.row];
    return cell;
}

- (void)viewWillAppear:(BOOL)animated {
//    NSLog(@"-------");
    [self.collectionView scrollToItemAtIndexPath:self.indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}


- (void)dismissAnimation {
    [self.tabBarController hiddenTabBar:NO];
    HLYiTanContentCollectionViewCell *cell = (HLYiTanContentCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:self.indexPath];
    self.snapShotView = [cell snapshotViewAfterScreenUpdates:YES];

    [UIView animateWithDuration:0.5 animations:^{
        self.collectionView.alpha = 0;
        self.snapShotView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
            self.snapShotView.frame = CGRectMake(50 * sWEIGHT, 80 * sHEIGHT, self.frameCell.size.width + 20, self.frameCell.size.height + 20);
        } completion:^(BOOL finished) {
            [self.navigationController popViewControllerAnimated:NO];
        }];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
